/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.system.entity;


import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 
 * @author chan
 * create  by chan on 2016-06-08 后台系统菜单树，通过权限标签控制，灵活性较差
 * modify by chan on 2016-09-01 后台系统菜单树，通过数据库查询生成（未解决缓存问题），
 * 												   理解权限即资源，加入type，区分菜单和按钮
 */
@Entity
@Table(name = "system_permission")
public class Permission implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Long id;
	private Permission parent;	// 父级菜单
	private String parentIds; 		// 所有父级编号
	private String name; 			// 名称
	private String description; 	// 描述
	private String url; 				// 链接
	private String state; 				// 显示（"opend"：显示；"close"：不显示）
	private Integer seq; 				// 排序
	private Integer type;				// 类型 1菜单 2按钮
	private String permission; 	// 权限标示  (如：system:user:list)
	private String menuParent; 	//FIXME 丑陋实现，之后需要修改 
	private String menuChild; 	
	
	
	
	private Integer status;
	private Corporation corp;
	private String creator;
	private Date createTime;
	
	
	
	/* ---------------- method ----------------*/
	
	public Permission(){
	}

	public Permission(Long id){
		this.id = id;
	}

	@JsonIgnore
	public static void sortList(List<Permission> list, List<Permission> sourcelist, Long parentId, boolean cascade){
		for (int i=0; i<sourcelist.size(); i++){
			Permission p = sourcelist.get(i);
			if (p.getParent()!=null && p.getParent().getId()!=null
					&& p.getParent().getId().equals(parentId)){
				list.add(p);
				if (cascade){
					// 判断是否还有子节点, 有则继续获取子节点
					for (int j=0; j<sourcelist.size(); j++){
						Permission child = sourcelist.get(j);
						if (child.getParent()!=null && child.getParent().getId()!=null && child.getParent().getId().equals(p.getId())){
							sortList(list, sourcelist,p.getId(), true);
							break;
						}
					}
				}
			}
		}
	}

	@JsonIgnore
	public static Long getRootId(){
		return 1L;
	}
	
	/* ---------------- getter/setter ----------------*/
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne
	@JoinColumn(name = "parent_id")
	public Permission getParent() {
		return parent;
	}

	public void setParent(Permission parent) {
		this.parent = parent;
	}

	@Column(name = "parent_ids")
	public String getParentIds() {
		return parentIds;
	}

	public void setParentIds(String parentIds) {
		this.parentIds = parentIds;
	}

	@Column(name = "permission")
	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name = "description")
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	@Column(name="menu_parent")
	public String getMenuParent() {
		return menuParent;
	}

	public void setMenuParent(String menuParent) {
		this.menuParent = menuParent;
	}

	@Column(name="menu_child")
	public String getMenuChild() {
		return menuChild;
	}

	public void setMenuChild(String menuChild) {
		this.menuChild = menuChild;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public Integer getSeq() {
		return seq;
	}

	public void setSeq(Integer seq) {
		this.seq = seq;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	@Column(name = "status")
	public Integer getStatus() {
		return status;
	}
	
	public void setStatus(Integer status) {
		this.status = status;
	}
	
	@ManyToOne  
    @JoinColumn(name="corp_id")
	public Corporation getCorp() {
		return corp;
	}

	public void setCorp(Corporation corp) {
		this.corp = corp;
	}
	
	@Column(name = "creator")
	public String getCreator() {
		return creator;
	}
	
	public void setCreator(String creator) {
		this.creator = creator;
	}
	
	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}
	
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	
}



